<?php
namespace Mageplaza\HelloWorld\Model\ResourceModel;


class Post extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        parent::__construct($context);
    }

    protected function _construct()
    {
        $this->_init('mageplaza_helloworld_post', 'post_id');
    }

    /**
     * 通过传递id从DB搜索帖子标题。
     *
     * @param string $id
     * @return string|bool
     */
    public function getPostNameById($id)
    {

        $adapter = $this->getConnection();
        $select = $adapter->select()
            ->from($this->getMainTable(), 'name')
            ->where('post_id = :post_id');
        $binds = ['post_id' => (int)$id];
        return $adapter->fetchOne($select, $binds);
    }

    /**
     * 保存之前回调
     *
     * @param \Magento\Framework\Model\AbstractModel|\Mageplaza\HelloWorld\Model\Post $object
     * @return $this
     */
    protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
    {
        $object->setUpdatedAt($this->_date->date());
        if ($object->isObjectNew()) {
            $object->setCreatedAt($this->_date->date());
        }
        return parent::_beforeSave($object);
    }
    protected  function  _getConnection($resourceName)
    {
        return parent::_getConnection($resourceName); // TODO: Change the autogenerated stub
    }

    public function getTest($arr){
        return $this->getConnection()->insert("mageplaza_helloworld_post",$arr);
    }
}